package 力扣日常刷题.木22一月.第14天0123;

/**
 * @author 帅小伙
 * @date 2022/1/23
 * @description
 */
public class Demo51动态规划两个非重叠子数组的最大和 {




    public static int maxSumTwoNoOverlap(int[] nums, int firstLen, int secondLen) {
        int ans = Integer.MIN_VALUE;

        int N = nums.length;
        int[] sum  = new int[N+1];

        for (int i = 0; i < N; i++) {
            sum[i+1] = sum[i] + nums[i];
        }

        for (int i = 0; i < N; i++) {
            for (int j = i+firstLen; j <= N - secondLen; j++) {
                int t = sum[i+firstLen] - sum[i] + sum[j+secondLen] - sum[j];
                ans = Math.max(ans,t);
            }
        }

        for (int i = 0; i < N; i++) {
            for (int j = i+secondLen; j <= N - firstLen; j++) {
                ans = Math.max(ans,sum[i+secondLen] - sum[i] + sum[j+firstLen] - sum[j]);
            }
        }
        return ans;
    }
}
